﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Common;
using Presentation.Module.Security.Interfaces.Services;
using System.Collections.ObjectModel;
using Presentation.Module.Security.Model;
using Application.Interfaces;
using AutoMapper;
using VPL.Mapper;
using Presentation.Module.Security.Service.Mapper;

namespace Presentation.Module.Security.Service
{
    public class UserService : IUserService
    {
        private readonly IUserManagementService _userManagementService;

        public UserService(IUserManagementService userManagementService)
        {
            if(userManagementService == null)
            {
                throw new ArgumentNullException("userManagementService");
            }

            this._userManagementService = userManagementService;
        }

        public ObservableCollection<User> GetAllUsers()
        {
            //var users = new ObservableCollection<User>();

            //var toModelMapper = new Mapper<User, Domain.UserManagement.Entities.User>();

            //var allUsers = this._userManagementService.GetAllUsers();

            //foreach ( var user in allUsers)
            //{
            //    users.Add(toModelMapper.Map(user));
            //}

            //return users;

            var users = new ObservableCollection<User>();

            var allUsers = this._userManagementService.GetAllUsers();

            foreach (var user in allUsers)
            {
                users.Add(UserManagementMapper.DomainToModel(user));
            }

            return users;
        }

        public User AddUser(User newUser)
        {
            //var toDomainMapper = new Mapper<Domain.UserManagement.Entities.User, User>();

            //var returnUser = this._userManagementService.AddUser(toDomainMapper.Map(newUser));

            ////var toDomainMapperRole = new Mapper<Domain.UserManagement.Entities.Role, Role>();
            ////var returnRole = toDomainMapperRole.Map(newUser.UserRole);

            //var toModelMapper = new Mapper<User, Domain.UserManagement.Entities.User>();

            //return toModelMapper.Map(returnUser);



            var returnUser = this._userManagementService.AddUser(UserManagementMapper.ModelToDomain(newUser));

            return UserManagementMapper.DomainToModel(returnUser);
        }

        public void EditUser(User user)
        {
            throw new NotImplementedException();
        }

        public void DeleteUser(User user)
        {
            throw new NotImplementedException();
        }

        public User FindUser(string userName)
        {
            var user = _userManagementService.FindUser(userName);

            if(user != null)
            {
                return UserManagementMapper.DomainToModel(user);
            }
            else
            {
                return null;
            }
        }

        public ObservableCollection<Role> GetAllRoles()
        {
            //var roles = new ObservableCollection<Role>();

            //var toModelMapper = new Mapper<Role, Domain.UserManagement.Entities.Role>();

            //var allRoles = this._userManagementService.GetAllRoles();

            //foreach (var role in allRoles)
            //{
            //    roles.Add(toModelMapper.Map(role));
            //}

            //return roles;

            var roles = new ObservableCollection<Role>();

            var allRoles = this._userManagementService.GetAllRoles();

            foreach (var role in allRoles)
            {
                roles.Add(UserManagementMapper.DomainToModel(role));
            }

            return roles;
        }

        public Role AddRole(Role newRole)
        {
            //var toDomainMapper = new Mapper<Domain.UserManagement.Entities.Role, Role>();

            //var returnRole = this._userManagementService.AddRole(toDomainMapper.Map(newRole));

            //var toModelMapper = new Mapper<Role, Domain.UserManagement.Entities.Role>();

            //return toModelMapper.Map(returnRole);


            var returnRole = this._userManagementService.AddRole(UserManagementMapper.ModelToDomain(newRole));

            return UserManagementMapper.DomainToModel(returnRole);
        }

        public void EditRole(Role role)
        {
            throw new NotImplementedException();
        }

        public void DeleteRole(Role role)
        {
            throw new NotImplementedException();
        }
    }
}
